<?php
/**
 * FeedbackModel.class.php
 * 意见管理模型
 *
 * @author   <王中艺>
 * @date     2017-02-18
 */

namespace Admin\Model;
use Think\Model;

class FeedbackModel extends Model {
	protected 	$trueTableName 	= 't_feedback';
	public 	$errMsg 	= false;

	/**
	 * 获取意见列表
	 * @param   $status 意见状态
	 * @param   $page  页码数
	 * @return  $list
	 */
	function getList($status, $page, &$pageHtml){
		$where 	= '1 = 1';
		if(isset($status) && in_array($status, array(0, 1)))
			$where 	.= ' and status = '.$status;

		$list 	= $this->where($where)
			->order('id desc')
			->page($page, C('PAGE_COUNT'))
			// ->cache(true)
			->select();
		foreach($list as &$item){
			$item['status_text']	= C('FEEDBACK_STATUS.'.$item['status']);
			$item['create_date']	= date('Y-m-d', $item['create_time']);
			$item['deal_date']	= $item['deal_time'] ? date('Y-m-d', $item['deal_time']) : '无效时间';
			$item['simple_content']	= cut_str($item['content'], 10);
		}

		$count 	= $this->where($where)->cache(true)->count();
		$Page 	= new \Think\Page($count, C('PAGE_COUNT'));
		$pageHtml	= $Page->angular_show();

		return $list;
	}

	/**
	 * 获取意见详情
	 * @param   $feedbackId 意见ID
	 * @return  $feedInfo
	 */
	function getDetail($feedId){
		$cacheKey 	= 'feedback_info_'.$feedId;
		$feedInfo 	= S($cacheKey);
		if($feedInfo) 	
			return $feedInfo;

		$feedInfo 	= M('t_feedback a', null)
			->join('left join t_user b on a.user_id = b.id')
			->join('left join ad_user c on a.dealer_id = c.uid')
			->where('a.id = %d', $feedId)
			->field('a.id, a.user_id, b.nickname, b.truename, a.mobile, a.content, a.create_time, a.dealer_id,
				c.username dealer_name, c.nickname dealer_nick, a.reward_score, a.status')
			->find();
		
		//字段处理
		$feedInfo['create_date']	= $feedInfo['create_time'] ? date('Y-m-d', $feedInfo['create_time']):'无效时间';
		$feedInfo['deal_date']	= $feedInfo['deal_time'] ? date('Y-m-d', $feedInfo['deal_time']) : '无效时间';
		$feedInfo['status_text'] 	= C('FEEDBACK_STATUS.'.$feedInfo['status']);
		S($cacheKey, $feedInfo);
		return $feedInfo;
	}

	/**
	 * 处理意见
	 * @param   $feedId 意见ID
	 * @param   $score 奖励积分数
	 * @param   $dealerId 处理人ID
	 * @return  boolean [description]
	 */
	function dealItem($feedId, $score, $dealerId){
		$info 	= array(
			'dealer_id'	=> $dealerId,
			'deal_time'	=> time(),
			'reward_score'	=> $score,
			'status'	=> 1
		);

		$result 	= $this
			->where('id = %d and status = %d', $feedId, 0)
			->save($info);
		if($result === false){
			$this->errMsg 	= '意见信息保存失败!(t_feedback)';
			return false;
		}
		//清除缓存
		S('feedback_info_'.$feedId, null);

		return true;
	}
}